ALTER PROCEDURE [dbo].[CPP_EfficacyLogonVisitor]
  @cbPlatformID TINYINT,          -- 登陆平台; 1=IOS; 2=Android; 3=PC; 4=网页; 5=微信小游戏; 6=QQ玩一玩;
  @strClientIP NVARCHAR(15),      -- 连接地址
  @strMachineID NVARCHAR(66),     -- 机器标识
  @Channel  INT,                  -- RYPlatformDB.dbo.GameChannel.ID
  @szResult NVARCHAR(255) OUTPUT  -- 输出信息
AS
BEGIN

  -- 属性设置
  SET NOCOUNT ON

  -- 基本信息
  DECLARE @UserID INT = 0
  DECLARE @CustomID INT
  DECLARE @FaceID SMALLINT
  DECLARE @Accounts NVARCHAR(31)
  DECLARE @NickName NVARCHAR(31)
  DECLARE @PlatformID TINYINT
  DECLARE @UserUin NVARCHAR(32)
  DECLARE @UnderWrite NVARCHAR(63)
  DECLARE @PlayTimeCount INT

  -- 积分变量
  DECLARE @Score BIGINT = 0   -- 身上金币
  DECLARE @Insure BIGINT = 0  -- 保险箱金币

  -- 扩展信息
  DECLARE @GameID INT = 0
  DECLARE @Gender TINYINT
  DECLARE @UserMedal INT
  DECLARE @Experience INT
  DECLARE @LoveLiness INT
  DECLARE @MemberOrder SMALLINT
  DECLARE @MemberOverDate DATETIME
  DECLARE @MemberSwitchDate DATETIME
  DECLARE @GrantScoreCount BIGINT = 0  -- 新用户注册系统送游戏币的数目

  -- 渠道号判断
  DECLARE @State INT = 0

  SELECT @State = State, @GrantScoreCount = ISNULL(RegZengCoin, 0) FROM RYPlatformDB.dbo.GameChannel WHERE ID = @Channel
  IF @@ROWCOUNT=0 OR @State<>1 BEGIN
    SET @szResult = N'渠道[' + CAST(@Channel AS NVARCHAR) + ']不存在或未启用';
    RETURN 3
  END

  DECLARE @EnjoinLogon INT = 0

  SELECT @EnjoinLogon=ISNULL(StatusValue, 0) FROM SystemStatusInfo(NOLOCK) WHERE StatusName=N'EnjoinLogon'
  IF @EnjoinLogon <> 0 BEGIN  -- 系统暂停
    SELECT @szResult = StatusString FROM SystemStatusInfo(NOLOCK) WHERE StatusName=N'EnjoinLogon'
    RETURN 2
  END

  -- 效验IP地址
  SELECT @EnjoinLogon=ISNULL(EnjoinLogon, 0) FROM ConfineAddress(NOLOCK) WHERE AddrString=@strClientIP AND GETDATE()<EnjoinOverDate
  IF @EnjoinLogon <> 0 BEGIN
    SET @szResult = N'抱歉地通知您，系统禁止了您所在的 IP 地址的登录功能，请联系客户服务中心了解详细情况！'
    RETURN 3
  END

  -- 效验机器
  SELECT @EnjoinLogon=ISNULL(EnjoinLogon, 0) FROM ConfineMachine(NOLOCK) WHERE MachineSerial=@strMachineID AND GETDATE()<EnjoinOverDate
  IF @EnjoinLogon <> 0 BEGIN
    SET @szResult = N'抱歉地通知您，系统禁止了您的机器的登录功能，请联系客户服务中心了解详细情况！'
    RETURN 4
  END

  DECLARE @DateID INT = CAST(CAST(GETDATE() AS FLOAT) AS INT)

  -- 查询用户
  DECLARE @Nullity TINYINT
  DECLARE @StunDown TINYINT
  DECLARE @LogonPass AS NCHAR(32)
  DECLARE @InsurePass AS NCHAR(32)
  DECLARE @MoorMachine AS TINYINT
  DECLARE @loginUserRight AS INT
  DECLARE @szDynamicPass NVARCHAR(32) -- 动态密码

  SELECT
    @UserID=ISNULL(UserID, 0), @GameID=ISNULL(GameID, 0), @Accounts=Accounts,
    @NickName=NickName, @PlatformID=PlatformID, @UserUin=UserUin, @UnderWrite=UnderWrite,
    @LogonPass=LogonPass, @FaceID=FaceID, @CustomID=CustomID, @Gender=Gender,
    @Nullity=Nullity, @StunDown=StunDown, @UserMedal=UserMedal, @Experience=Experience,
    @InsurePass=InsurePass, @LoveLiness=LoveLiness, @MemberOrder=MemberOrder,
    @MemberOverDate=MemberOverDate, @MemberSwitchDate=MemberSwitchDate,@loginUserRight = UserRight,
    @MoorMachine=MoorMachine, @PlayTimeCount=PlayTimeCount,
    @szDynamicPass=DynamicPass
  FROM AccountsInfo(NOLOCK) WHERE IsAndroid = 2 AND Channel = @Channel AND RegisterMachine = @strMachineID

  -- 注册用户
  IF @UserID > 0 BEGIN
    UPDATE AccountsInfo SET
      GameLogonTimes=GameLogonTimes+1, LastLogonDate=GETDATE(),
      LastLogonIP=@strClientIP,LastLogonMachine=@strMachineID
    WHERE UserID = @UserID
  
  END ELSE BEGIN

    -- 分配标识
    SELECT @GameID = ISNULL(Code, 0) FROM RYWebSMSDB.dbo.CodeIdentifier WHERE AccountsUsed = 0 ORDER BY ID ASC
    IF @GameID = 0 BEGIN
      SET @szResult = N'系统分配游戏标识失败'
      RETURN 4;
    END
  
    -- 临时账号
    DECLARE @strTemp NVARCHAR(31) = CONVERT(NVARCHAR(31),REPLACE(NEWID(),'-','_'))

    -- 注册用户
    INSERT AccountsInfo (
      Accounts,NickName,RegAccounts,PlatformID,UserUin,LogonPass,InsurePass,
      Gender,FaceID, GameLogonTimes,LastLogonIP,LastLogonMobile,LastLogonMachine,RegisterIP,
      RegisterMobile,RegisterMachine, GameID, IsAndroid, LastLogonDate, RegisterDate,Channel
    ) VALUES (
      @strTemp,@strTemp,@strTemp,@cbPlatformID,@strMachineID,N'd1fd5495e7b727081497cfce780b6456', N'21218cca77804d2ba1922c33e0151105',
      0,0, 1,@strClientIP,N'',@strMachineID, @strClientIP,
      N'',@strMachineID, @GameID, 2, GETDATE(), GETDATE(),@Channel
    )
    SELECT @UserID = @@IDENTITY;
    
    UPDATE RYWebSMSDB.dbo.CodeIdentifier SET AccountsUsed = @UserID WHERE Code = @GameID

    --更新账号昵称
    UPDATE AccountsInfo SET NickName = N'游客'+CONVERT(NVARCHAR(31), @UserID) WHERE UserID = @UserID

    -- 查询用户
    SELECT
      @UserID=UserID, @GameID=GameID, @Accounts=Accounts, @NickName=NickName,
      @UserUin=UserUin, @UnderWrite=UnderWrite, @LogonPass=LogonPass,
      @FaceID=FaceID, @CustomID=CustomID, @Gender=Gender, @Nullity=Nullity,
      @StunDown=StunDown, @UserMedal=UserMedal, @Experience=Experience,
      @LoveLiness=LoveLiness, @MemberOrder=MemberOrder, @MemberOverDate=MemberOverDate,
      @MemberSwitchDate=MemberSwitchDate,
      @MoorMachine=MoorMachine,@PlayTimeCount=PlayTimeCount
    FROM AccountsInfo(NOLOCK) WHERE IsAndroid = 2 AND Channel = @Channel AND RegisterMachine = @strMachineID

    -- 注册赠送金币
    IF @GrantScoreCount>0 BEGIN

      -- 查询金币
      DECLARE @CurrScore BIGINT = 0 -- 身上金币
      DECLARE @CurrInsure BIGINT =0 -- 保险箱金币
      SELECT @CurrScore=ISNULL(Score, 0), @CurrInsure=ISNULL(InsureScore, 0) FROM RYTreasureDBLink.RYTreasureDB.dbo.GameScoreInfo WHERE UserID=@UserID
      IF @@ROWCOUNT = 0 BEGIN
        -- 赠送金币
        INSERT RYTreasureDBLink.RYTreasureDB.dbo.GameScoreInfo (UserID, Score, RegisterIP, LastLogonIP)
        VALUES (@UserID, @GrantScoreCount, @strClientIP, @strClientIP)
      END

      -- 用户流水账
      INSERT INTO RYTreasureDBLink.RYTreasureDB.dbo.RecordPresentInfo(UserID,PreScore,PreInsureScore,PresentScore,TypeID,IPAddress,CollectDate)
      VALUES (@UserID,@CurrScore,@CurrInsure,@GrantScoreCount,1,@strClientIP,GETDATE())

      -- 用户日统计
      UPDATE RYTreasureDBLink.RYTreasureDB.dbo.StreamPresentInfo SET
        DateID=@DateID, PresentCount=PresentCount+1,PresentScore=PresentScore+@GrantScoreCount
      WHERE UserID=@UserID AND TypeID=1
      
      IF @@ROWCOUNT=0 BEGIN
        INSERT RYTreasureDBLink.RYTreasureDB.dbo.StreamPresentInfo(DateID,UserID,TypeID,PresentCount,PresentScore)
        VALUES(@DateID,@UserID,1,1,@GrantScoreCount)
      END
    END

  END

  -- 帐号禁止
  IF @Nullity<>0 BEGIN
    SET @szResult = N'您的帐号暂时处于冻结状态，请联系客户服务中心了解详细情况！'
    RETURN 5
  END

  -- 帐号关闭
  IF @StunDown<>0 BEGIN
    SET @szResult = N'您的帐号使用了安全关闭功能，必须重新开通后才能继续使用！'
    RETURN 6
  END

  -- 固定机器
  IF @MoorMachine <> 0 BEGIN
    SET @szResult = N'您的帐号使用固定机器登录功能，您不能使用手机终端进行登录！'
    RETURN 7
  END

  -- 查询身上金币和保险箱金币
  SELECT @Score = ISNULL(Score, 0), @Insure=ISNULL(InsureScore, 0) FROM RYTreasureDBLink.RYTreasureDB.dbo.GameScoreInfo WHERE UserID = @UserID

  -- 判断玩家是否在房间中，如果在房间中不更新动态密码
  DECLARE @LockServerIDCheck INT = 0
  SELECT @LockServerIDCheck = ISNULL(ServerID, 0) FROM RYTreasureDBLink.RYTreasureDB.dbo.GameScoreLocker WHERE UserID = @UserID
  IF @LockServerIDCheck = 0 BEGIN
    SET @szDynamicPass = CONVERT(nvarchar(32),REPLACE(newid(),'-',''))
    UPDATE AccountsInfo SET DynamicPassTime = GETDATE(), DynamicPass = @szDynamicPass WHERE UserID=@UserID
  END

  DECLARE @InsureEnabled TINYINT = 0 -- 银行密码为空提示
  IF @InsurePass <> '' BEGIN
    SET @InsureEnabled = 1
  END
  
  -- 输出变量
  SELECT @UserID AS UserID, @GameID AS GameID, @Accounts AS Accounts, @NickName AS NickName,@szDynamicPass AS DynamicPass,
    @UnderWrite AS UnderWrite,@FaceID AS FaceID, @CustomID AS CustomID, @Gender AS Gender, @UserMedal AS Ingot,
    @Experience AS Experience,@Score AS Score, @Insure AS Insure, @LoveLiness AS LoveLiness, @MemberOrder AS MemberOrder,
    @MemberOverDate AS MemberOverDate,@MoorMachine AS MoorMachine, @InsureEnabled AS InsureEnabled, @cbPlatformID AS LogonMode,
    @LogonPass AS LogonPass, '' AS WxImageUrl,@loginUserRight as UserRight
  
   SET @szResult = N'SUCCESS'
  RETURN 0
END